package com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

import struct.CommentInfo;
import struct.ItemInfo;
import struct.SellerInfo;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class ShowBuyerBidsServlet extends HttpServlet 
{//client-server style (get input from client, then redirect output)
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException 
	{
		String username = req.getParameter("username");
		
		req.setAttribute("username", username);
		req.setAttribute("itemList", GetItemInfo(username));
		
		
		
		req.getRequestDispatcher("yourBids.jsp").forward(req, resp);
	}
	
	public ArrayList<ItemInfo> GetItemInfo(String username)
	{
		java.util.Date d1,d2,d3;
		long minutes=-1;
		long hours=-1;
		long days=-1;
		String current="";
		String start = "";
		String end = "";
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
		
		java.util.Date currentDate = new java.util.Date();	
		current = df.format(currentDate);
		
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		
		ArrayList<ItemInfo> list = new ArrayList<ItemInfo>();

		//get all auctions associated with this current user
		Query qBids = new Query("Bid").addFilter("BIDDER", FilterOperator.EQUAL, username ).addSort("TIME", Query.SortDirection.DESCENDING);
		Iterable<Entity> bids = ds.prepare(qBids).asIterable();
		for(Entity bid : bids )
		{
			Entity e;
			try {
				e = ds.get(KeyFactory.createKey("Auction", Integer.parseInt((String) bid.getProperty("ITEMID"))));
				
				ItemInfo item = new ItemInfo();	
				
				item.itemID = Long.parseLong((String) bid.getProperty("ITEMID"));		
				item.itemName = (String) e.getProperty("NAME");		
				item.description = (String) e.getProperty("DESCRIPTION");		
				
				//get highest bid 
				Query qqBids = new Query("Bid").addFilter("ITEMID", FilterOperator.EQUAL, Long.toString(item.itemID) );
				Iterable<Entity> bbids = ds.prepare(qqBids).asIterable();
				double maxBid =Double.parseDouble( e.getProperty("PRICE").toString());
				double currBid;
				for(Entity bbid : bbids )
				{
					currBid = Double.parseDouble(bbid.getProperty("OFFER").toString());
					if(currBid > maxBid){
						maxBid = currBid;
					}
				}
				item.currentBid = maxBid;
				item.yourBid = Double.parseDouble(bid.getProperty("OFFER").toString());
				
				start =  (String) e.getProperty("START");
				end =  (String) e.getProperty("END");
				try {
					
					
					d1 = df.parse(start);
					d2 = df.parse(end);
					d3 = df.parse(current);
					
					 if (d3.after(d2)){
							//do not display, expired
					}else if(d3.before(d1)){
						minutes = (d2.getTime()-d1.getTime())/(1000*60)%60;
						hours = (d2.getTime()-d1.getTime())/(1000*60*60)%24;
						days = (d2.getTime()-d1.getTime())/(1000*60*60*24);
						item.timeLeft = (days + "d" + hours + "h" +minutes+"m");
						
						item.img = "ta1.jpg";
						item.seller = (String) e.getProperty("SELLER");
						
						list.add(item);

					}else{
						minutes = (d2.getTime()-d3.getTime())/(1000*60)%60;
						hours = (d2.getTime()-d3.getTime())/(1000*60*60)%24;
						days = (d2.getTime()-d3.getTime())/(1000*60*60*24);
						item.timeLeft = (days + "d" + hours + "h" +minutes+"m");
						
						item.img = "ta1.jpg";
						item.seller = (String) e.getProperty("SELLER");
						
						list.add(item);

					}
					
				} catch (ParseException ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				}

				
			} catch (NumberFormatException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			} catch (EntityNotFoundException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			}
			
			
		}
								
		return list;
	}
		
}
